package com.cpe.transfer.physique;

import javax.annotation.Resource;

import org.javatuples.Pair;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.cpe.data.domain.physique.score.PhysiqueScore;
import com.cpe.data.domain.physique.score.PhysiqueScoreInfo;
import com.cpe.data.domain.physique.score.PhysiqueScoreQuery;
import com.cpe.repository.mapper.physique.PhysiqueScoreMapper;

@Component
public class PhysiqueScoreTransfer {

	@Resource
	private PhysiqueScoreMapper physiqueScoreMapper;

	/**
	 * 实体-->响应
	 */
	public PhysiqueScoreInfo toInfo(PhysiqueScore physiqueScore) {
		Assert.notNull(physiqueScore);
		PhysiqueScoreInfo info = new PhysiqueScoreInfo();
		BeanUtils.copyProperties(physiqueScore, info);
		info.setLowOrHigh(2);
		return info;
	}

	/**
	 * 查询体型项目的分值，并计算出偏高还是偏低
	 * 
	 * @return
	 */
	public Pair<Integer, Integer> calcShapeValue(Float heightScore, Float wigthScore, Integer sex, Float age) {
		Assert.notNull(heightScore);
		Assert.notNull(wigthScore);
		Assert.notNull(sex);
		Assert.notNull(age);

		Integer value = 5;
		Integer lowOrHigh = 2;

		PhysiqueScoreQuery query = new PhysiqueScoreQuery();
		query.setAge(age);
		query.setPhysiqueName("体型");
		query.setScore(heightScore);
		query.setSex(sex);
		query.setValue(5);
		PhysiqueScore physiqueScore = physiqueScoreMapper.findOneByCustomQuery(query);
		if (physiqueScore != null) {
			if (wigthScore < physiqueScore.getMinWeight()) {
				lowOrHigh = 0;
			} else if (wigthScore > physiqueScore.getMaxWeight()) {
				lowOrHigh = 1;
			}
		}

		query.setValue(null);
		query.setScore2(wigthScore);
		physiqueScore = physiqueScoreMapper.findOneByCustomQuery(query);
		if (physiqueScore != null) {
			value = physiqueScore.getValue();
		}

		return Pair.with(value, lowOrHigh);
	}
}
